package com.hafu.view;

import com.hafu.model.KuCun;
import com.hafu.service.KuCunService;

import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author Administrator
 */
public class KuCunFrm extends JFrame {
    private KuCunService kuCunService = new KuCunService();

    public KuCunFrm() throws SQLException {
        initComponents();
        // 填充表格数据
        fillTableData();
    }

    private void fillTableData() throws SQLException {
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        // 获取数据
        ArrayList<KuCun> list = kuCunService.getKuCunList();
        for (KuCun kuCun : list) {
            Vector<Object> rowData = new Vector<>();
            rowData.add(kuCun.getId());
            rowData.add(kuCun.getSpname());
            rowData.add(kuCun.getNum());
            model.addRow(rowData);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("库存详情");
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null},
                            {null, null, null},
                    },
                    new String[] {
                            "编号", "名称", "数量"
                    }
            ));
            scrollPane1.setViewportView(table1);
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(135, 135, 135)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 354, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(154, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 370, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(13, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
   